package com.fate.controller;

import com.fate.domain.Category;
import com.fate.service.CategoryService;
import com.netflix.discovery.converters.Auto;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.checkerframework.checker.units.qual.C;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.RequestEntity;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.ws.rs.GET;
import java.util.List;

@RestController
@RequestMapping("prod/category")
@Api(tags = "商品分类管理")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    /**
     * 全查询商品分类
     */
    @GetMapping("table")
    @PreAuthorize("hasAuthority('prod:category:page')")
    @ApiOperation("全查询商品分类")
    public ResponseEntity<List<Category>> findCategoryPage() {
        List<Category> categories = categoryService.list();
        return ResponseEntity.ok(categories);
    }


    /**
     * 查询商品父级分类
     */
    @GetMapping("listCategory")
    @PreAuthorize("hasAuthority('prod:category:page')")
    @ApiOperation("查询商品父级分类")
    public ResponseEntity<List<Category>> findCategoryParent() {
        List<Category> categoryList = categoryService.findCategoryParent();
        return ResponseEntity.ok(categoryList);
    }


    /**
     * 新增商品分类
     *
     * @return
     */
    @PostMapping
    @PreAuthorize("hasAuthority('prod:category:save')")
    @ApiOperation("新增商品分类")
    public ResponseEntity<Void> addCategory(@RequestBody Category category){
        categoryService.save(category);
        return ResponseEntity.ok().build();
    }
}
